package com.sx.pipeline.ui.workbench.frag

import android.graphics.Color
import android.os.Bundle
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.google.android.material.tabs.TabLayout
import com.sx.mvvmlibs.utils.Logs
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.databinding.FragTaskBinding
import com.sx.pipeline.databinding.ViewCustomTabBinding
import com.sx.pipeline.repos.http.res.TaskStatisticsVO

/**
 * @Author: YY
 * @Date: 2022/8/2
 * @Desc: TODO
 */
class TaskFragment: CYDataBindingBaseFragment<FragTaskBinding, TaskVM>(R.layout.frag_task) {

    private var position: Int = 0

    companion object {
        fun newInstance(position: Int) =
            TaskFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, position)
                }
            }
    }

    override fun initParam() {
        super.initParam()
        if (arguments != null) {
            arguments?.let {
                position = it.getInt(IntentKey.ID)
            }
        }
    }

    override fun initListener() {
        // 本月、年度
        mBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

            override fun onTabSelected(tab: TabLayout.Tab?) {
                mViewModel.getTaskStatisticsData(position, tab!!.position)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })
    }

    override fun initData() {
        mViewModel.getTaskStatisticsData(position, 0)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 任务统计
        setTaskStatistics()
    }


    /**
     *  任务统计
     */
    private fun setTaskStatistics() {
        observe(mViewModel.taskStatisticsEvent) {
            if (!it.isNullOrEmpty()) {
                initTabs(it[0]!!.sumQty, it[1]!!.sumQty, it[2]!!.sumQty, it)
            }
        }
    }

    /**
     * 任务Tab
     */
    private fun initTabs(releaseNum: Int, partakeNum: Int, examine: Int, mapData: HashMap<Int, TaskStatisticsVO>) {
        mBinding.tab.clearOnTabSelectedListeners()
        mBinding.tab.removeAllTabs()
        // 任务类型（发布、参与、审批）
        mViewModel.getTaskTabsList(releaseNum, partakeNum, examine).forEachIndexed { index, _ ->
            val binding = DataBindingUtil.inflate<ViewCustomTabBinding>(
                layoutInflater, R.layout.view_custom_tab, null, false
            )
            binding.tvTabText.text = mViewModel.getTaskTabsList(releaseNum, partakeNum, examine)[index]
            val tab: TabLayout.Tab = mBinding.tab.newTab().setCustomView(binding.root)
            mBinding.tab.addTab(tab)

            if (index == 0) {
                binding.tvTabText.setTextColor(Color.parseColor("#0188FF"))
                mViewModel.getUndoneTask()
                updateView(0, mapData)
            }
        }
        mBinding.tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val tabText: TextView = tab!!.customView!!.findViewById(R.id.tv_tab_text)
                tabText.setTextColor(Color.parseColor("#0188FF"))
                Logs.d("onTabSelected tab ${tab.position}")
                updateView(tab.position, mapData)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                val tabText: TextView = tab!!.customView!!.findViewById(R.id.tv_tab_text)
                tabText.setTextColor(Color.parseColor("#666666"))

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })
    }

    /**
     * 更新数据View
     */
    private fun updateView(index: Int, mapData: HashMap<Int, TaskStatisticsVO>) {
        Logs.d("updateView ${mapData[index]!!.completeTaskQty}")
        when(index) {
            0 -> {
                mBinding.tvTitle.text = "我发布的"
            }
            1 -> {
                mBinding.tvTitle.text = "我参与的"
            }
            2 -> {
                mBinding.tvTitle.text = "我审批的"
            }
        }
        // 直接设置String类型的数据
        mBinding.ringView.setAnglesData(
            "${mapData[index]!!.undoneTaskQty}",
            "${mapData[index]!!.completeTaskQty}",
            "${mapData[index]!!.postponeTaskQty}"
        )
        // 未完成 已完成 已延期
        mBinding.ringView.initPaint("#FEBB30", "#27C83E", "#FF5E57")
        mBinding.ringView.showViewWithoutAnimation() //不带动画展示圆环

        // 未完成
        mBinding.tvUnfinishedValue.text = "${mapData[index]!!.undoneTaskQty}"
        // 已完成
        mBinding.tvCompletedValue.text = "${mapData[index]!!.completeTaskQty}"
        // 已延期
        mBinding.tvDelayValue.text = "${mapData[index]!!.postponeTaskQty}"
    }
}